<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:05
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\model\mysql;

use app\common\lib\Status;

class Order extends Base
{
    /**
     * @param int $limit
     * @param string $openid
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getStaffByOpenidList(int $limit = 10, string $openid = "")
    {
        return $this->field("*")
            ->where('fatherOpenid', $openid)
            ->where('status', Status::mysql_normal)
            ->paginate($limit)
            ->toArray();
    }

    /**
     * 查询用户该演出的订单张数
     * @param string $openid
     * @param int $theatre_id
     * @return Order|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderByOpenidCount(string $openid = "", int $theatre_id = 0)
    {
        return $this->field("count(*) as count")
            ->where('openid', $openid)
            ->where('theatre_id', $theatre_id)
            ->find();
    }

    /**
     * 根据订单号查询订单
     * @param string $order_id
     * @param string $field
     * @return Order|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderByOrderId(string $order_id = '', string $field = '*')
    {
        return $this->field($field)
            ->where('order_id', $order_id)
            ->find();
    }

    /**
     * @param string $openid
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderByOpenid(string $openid = "", string $field = "*"): array
    {
        return $this->field($field)
            ->where('openid', $openid)
            ->where('status', Status::mysql_normal)
            ->order('id desc')
            ->select()
            ->toArray();
    }

    /**
     * @param string $openid
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderListInfo(string $openid = '', int $type = 0, string $field = '*')
    {
        return $this->field($field)
            ->where('openid', $openid)
            ->where('state', $type)
            ->where('status', Status::mysql_normal)
            ->order('theatre_time asc,id desc')
            ->select()
            ->toArray();
    }

    public function getOrderListInfoNotPay(string $openid = '', int $type = 0, string $field = '*')
    {
        return $this->field($field)
            ->where('openid', $openid)
            ->where('payStatus', 0)
            ->order('theatre_time asc,id desc')
            ->select()
            ->toArray();
    }

    public function getGetTheatreIdDataAttr($value)
    {
        $theatre = (new Theatre())->getBaseById((int)$value, '*,venue as get_venue,thumb as root_path_thumb,theatre_cate_id as get_theatre_cate_id,seat_id as get_seat_id,start_time as start_day,start_time as start_day_time,end_time as end_day_time,start_time as start_date_a');
        if (!$theatre) {
            return [];
        }
        return $theatre->toArray();
    }

    public function getGetTheatreTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function getGetStartTimeAttr($value)
    {
        return date('Y-m-d', $value);
    }

    /**
     * @param $value
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getGetOpenidUserAttr($value)
    {
        $wechat = (new Wechat())->getWechatByOpenid($value);
        if (!$wechat) {
            return [];
        }
        return $wechat->toArray();
    }

    public function getGetDetailDataCodeAttr($value)
    {
        $detail = (new OrderDetail())->where('order_id', $value)
            ->select()
            ->toArray();
        if (empty($detail)) {
            return [];
        }

        $str = "";
        foreach ($detail as $k => $value) {
            $str .= $value['code'] . " ";
        }
        return $str;
    }

    /**
     * @param int $time
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderInvalid(int $time = 0): array
    {
        return $this->where('theatre_time', '<', $time)
            ->where('state', 1)
            ->where('payStatus', 1)
            ->select()
            ->toArray();
    }

    /**
     * @param array $ids
     * @return Order
     */
    public function updateByIdsInvalid(array $ids = [])
    {

        return $this->where('id', 'in', $ids)
            ->update([
                "state" => 3,
                "update_time" => time()
            ]);
    }
}